#!/bin/bash 


echo \'开始执行安装...\'

/usr/local/mysql/support-files/mysql.server stop
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock

# MySQL 安装日志信息
rm -rf /var/log/
mkdir  /var/log/

touch /var/log/mysql_install.log
chmod 644 /var/log/mysql_install.log
log=/var/log/mysql_install.log

# MySQL压缩包路径
MySQLPath=./mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
# sql 文件
sqlfile=/home/tuners/mysql-shellV1.0/test.sql
sqlfile1=/home/tuners/mysql-shellV1.0/adapterClient.sql

#如果文件夹不存在，则解压
tempPath="/usr/local/mysql-5.7.38-linux-glibc2.12-x86_64"
if [ ! -d "$tempPath" ]; then
# 解压MYSQL安装包
echo \'====================================\'
echo \'Unzip....\'
tar -zxf $MySQLPath -C /usr/local/
fi


rm -rf /usr/local/mysql
mkdir /usr/local/mysql
cp -r -p -a /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/* /usr/local/mysql
if [ $? -ne 0 ];then
echo \'MySQL unzip failed!!!\'
exit
else
echo \'====================================\'
echo \'Unpack the success!\'
fi

# 创建MYSQL⽤户和⽤户组
groupadd mysql
useradd -g mysql mysql

rm -rf /data/mysql
mkdir  -p /data/mysql
chown -R mysql:mysql /data/mysql
echo \'====================================\'
echo \'User created successfully!\'

rm -rf /etc/my.cnf
touch /etc/my.cnf
chmod 644  /etc/my.cnf

# 重写MYSQL的配置⽂件
cat > /etc/my.cnf << EOF
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
skip-grant-tables
EOF

if [ $? -ne 0 ];then
echo \'Failed to write the my.cnf configuration file!!!\'
exit
else
echo \'====================================\'
echo \'my.cnf configuration success!\'
echo \'====================================\'
fi



#安装 : libaio.so
dpkg -i ./mysql-shellV1.0/lib/*.deb

# 创建 /var/lib/mysql ⽬录，并修改权限   ?????
#mkdir -p /var/lib/mysql
#chmod 777 /var/lib/mysql

cd /data/mysql
rm -rf *

#执行初始化
#/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql 2>&1 | tee $log

chmod 644 /data/mysql/mysql.err
chmod 644 /data/mysql/mysql.pid

#证书
#sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 40976EAF437D05B5 3B4FE6ACC0B21F32
#添加源
#sed '$a\deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse' /etc/apt/sources.list
#更新源
#sudo apt update
#安装 sysv-rc-conf 
#apt-get install sysv-rc-conf


#是否可以删除
#sysv-rc-conf --level 2345 mysql off

#启动mysql
/usr/local/mysql/support-files/mysql.server stop
/usr/local/mysql/support-files/mysql.server start


#安装网络相关依赖包
#apt install net-tools

# 检查 MySQL 服务是否启动成功
#netstat -ntlp | grep 3306
ps -ef|grep mysql 


#MYSQL加入自启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
cd /etc/init.d
update-rc.d mysql defaults 

if [ $? -ne 0 ];then
echo 'MySQL Boot failure!!!'
exit
else
echo '===================================='
echo 'start MySQL successfully!'
fi

# 将 MYSQL 的 BIN ⽬录加⼊ PATH 环境变量
touch ~/.bash_profile

cat >  ~/.bash_profile  <<  EOF
"export PATH=\$PATH:/usr/local/mysql/bin" 
EOF
source ~/.bash_profile
# root用户添加mysql环境
echo "export PATH=\$PATH:/usr/local/mysql/bin" >> ~/.bashrc
source ~/.bashrc
#当前登陆用户添加mysql环境变量
echo "export PATH=\$PATH:/usr/local/mysql/bin" >> /home/tuners/.bashrc
source /home/tuners/.bashrc

if [ $? -ne 0 ];then
echo 'Variable write failed!!!'
exit
else
echo '===================================='
echo 'Variable write successful!'
echo '===================================='
fi

# 执行 SQL 语句更改密码
/usr/local/mysql/bin/mysql -uroot -p123456 -e"source /home/tuners/adapterClient/mysql-shellV1.0/adapterClient.sql"
/usr/local/mysql/bin/mysql -uroot -p123456 -e"source /home/tuners/adapterClient/mysql-shellV1.0/updateKey.sql"


#删除my.cnf文件中跳过密码登陆的内容
sed -i '14d' /etc/my.cnf

if [ $? -ne 0 ];then
echo 'fail to perform MySQL operation!!!'
exit
else
echo '===================================='
echo 'All operations were successful and can be connected through Navicat!!!'
echo '===================================='
fi

#重新启动mysql
service mysql restart

/usr/local/mysql/bin/mysql -uroot -pTuners2012\!\@\# -e"source /home/tuners/adapterClient/mysql-shellV1.0/updateRuler.sql" --connect-expired-password

sudo service mysql status


